package com.newgrand.annexserver.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.newgrand.annexcommon.constant.StatusConst;
import com.newgrand.annexserver.beans.Dbconfig;
import com.newgrand.annexserver.mapper.annex.DbconfigMapper;
import com.newgrand.annexserver.service.DbconfigService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 业务端连接及存储策略配置 服务实现类
 * </p>
 *
 * @author minyang
 * @since 2019-09-06
 */
@Service
public class DbconfigServiceImpl extends ServiceImpl<DbconfigMapper, Dbconfig> implements DbconfigService {

    @Autowired
    DbconfigMapper dbconfigMapper;

    public Pair<Boolean, String> addDBConfig(String dbtoken, Dbconfig dbconfig) {
        dbconfig.setDbtoken(dbtoken);
        dbconfig.setDbtoken(dbtoken);
        dbconfig.setCreatetime(new Date());
        dbconfig.setState(StatusConst.STATE_ENALBE);
        dbconfigMapper.insert(dbconfig);
        return Pair.of(true, "");
    }

    public List<Dbconfig> getDBConfigList(int state) {
        List<Dbconfig> dbconfigList = dbconfigMapper.selectList(new EntityWrapper<Dbconfig>().eq("state", StatusConst.STATE_ENALBE));
        return dbconfigList;
    }

    public Dbconfig getDBConfigByDbToken(String dbToken) {
        List<Dbconfig> dbconfigList = dbconfigMapper.selectList(new EntityWrapper<Dbconfig>().eq("dbtoken", dbToken).eq("state", StatusConst.STATE_ENALBE));
        if (dbconfigList != null && dbconfigList.size() > 0) {
            return dbconfigList.get(0);
        }
        return null;
    }
}
